#!/usr/bin/perl -w
use strict;

#Usage:
#  seq_pairs_align [OPTIONS] < SEQUENCES.FASTA > ALIGNMENT.RESULTS
#                  
#Optional Arguments:
#  -a, --allvsall                      : All vs all comparisons.
#  -p, --pairs_file FILE               : Tab-delimited sequence identifiers
#                                        pairs.

sub print_out(\%)
{
    my ($data) = @_;

    print <<"USAGE";

---+$data->{exe_name}
           <code>/net/cpp-group/Leo/bin/<big>$data->{exe_name}</big></code> 

.

---++<small>Part of Alignment / PAML pipeline :-<br></small>


USAGE
    for (@{$data->{description}})
    {
        print "   * ".$_."\n";
    }
    print <<"USAGE";

.

---++Programme Options
|*Main:*||
USAGE
    for my $option(@{$data->{optional}})
    {
        if ($option->[0] ne substr($option->[1],0,1))
        {
            $option->[0] = " or =-$option->[0]= ";
        }
        else
        {
            $option->[0] = "";
        }
        print <<"USAGE";
| =--$option->[1]=$option->[0] | $option->[2] |
USAGE
    }

    for my $option(@{$data->{mandatory}})
    {
        if ($option->[0] ne substr($option->[1],0,1))
        {
            $option->[0] = " or =-$option->[0]= ";
        }
        else
        {
            $option->[0] = "";
        }
        print <<"USAGE";
|*(Mandatory)* <br> =--$option->[1]=$option->[0] | $option->[2] |
USAGE
    }

    print <<"USAGE";

.

---++Useful Regular Expressions
  ="(ENS[A-Z]*G\\d+)"= to specify Ensembl gene IDs.<br>
  ="[^|]+\\|([^|]+)\\|"= for nr.<br>
  Your command shell may require you to escape backslashes e.g. ="\\\\"=

.

---+++ Usage:
   * <code> /net/cpp-group/Leo/bin/$data->{usage}[0]</code><br>

.

<small>Back to [[ReadyToRun]]</small>

USAGE

}

sub do_parse(\@\%)
{
    my ($lines, $data) =@_;

    die unless $lines->[0] eq 'Usage:';
    shift @$lines;
    $lines->[0] =~ /([^ ]+)/;
    $data->{exe_name} = $1;
    my $mode = 'usage';
    for (@$lines)
    {
        if (/Mandatory Arguments:/)
        {
            $mode = 'mandatory';
            next;
        }
        elsif (/Optional Arguments/)
        {
            $mode = 'optional';
            next;
        }
        elsif (/Description/)
        {
            $mode = 'description';
            next;
        }
        if ($_ =~ /^( +)(.*) ? *$/ && length ($1) > 2 && @{$data->{$mode}})
        {
            $data->{$mode}[-1] .= ' '. $2;
        }
        else
        {
            push(@{$data->{$mode}}, $2);
        }
    }

    # remove double spaces
    for (qw (optional mandatory description))
    {
        for my $line (@{$data->{$_}})
        {

            $line =~ s/ +/ /g;
        }
    }

    my %ignored_options;
    @ignored_options{qw(verbose version help err_log)} =();

  # parse options
    for my $key(qw (optional mandatory))
    {
        my $options;
        for (@{$data->{$key}})
        {

            /-(.), --([^ ]*)[^:]*: (.*)/ or die $_;
            next if (exists ($ignored_options{$2}));
            push(@{$options}, [$1,$2,$3]);
        }
        $data->{$key} = $options;
    }


}


while (<DATA>)
{
    my @lines;
    if  (/^\-+$/)
    {
        while (<DATA>)
        {
            chomp;
            next unless length $_;
            last if /^\-+$/;
            push(@lines, $_);
        }
    }
    next unless @lines;
    my %data;
    do_parse(@lines, %data);
    #debug print out
#    print "description\n";
#    print join ("\n", @{$data{description}}), "\n";
#    print "optional\n";
#    for (@{$data{optional}})
#    {
#        print "$_->[0], $_->[1], $_->[2]\n";
#    }
#    print "mandatory\n";
#    for (@{$data{mandatory}})
#    {
#        print "$_->[0], $_->[1], $_->[2]\n";
#    }
    print_out(%data);

}

__DATA__
--------------------------------------------------------------------------------

Usage:
  seq_pairs_list_identifiers [OPTIONS] < SEQUENCE_PAIR.RESULTS

Optional Arguments:
  -m, --map_prot_to_gene_to_species FILE : Contains sequence identifiers in the
                                           first column,tab-separated from the
                                           second column of gene identifiers,
                                           with species names in the third.
  -s, --summary FILE                     : Summary of how many unique
                                           identifiers for each species. Repeats
                                           information found in --verbose.
  -g, --genes                            : The identifiers are genes and do not
                                           need to be translated.
  -o, --output_path PATH                 : List of gene identifiers. If species
                                           names are given, identifiers will be
                                           written to species-specific files.
                                           (The species name will be appended to
                                           the file name before the extension.)
  -e, --err_log PATH (=STDERR)           : File for logging errors.
  -v, --verbose                          : Issue lots more messages.
  -V, --version                          : Print version.
  -h, --help                             : Display this message.

Description:
  Takes sequence pair results and counts the number of and outputs all unique
        identifiers.

--------------------------------------------------------------------------------

